/**
 * 详情对话框
 */
var InventoryPriceInfoDlg = {
    data: {
        shopTreeId: "",
        shopName: "",
        itemTypeCode: "",
        itemTypeName: "",
        itemid: "",
        itemName: "",
        price: "",
        id: ""
    }
};

layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/inventoryPrice/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('inventoryPriceForm', result.data);

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/inventoryPrice/editItem", function (data) {
            if(data.success){
                Feng.success("更新成功！");
                window.location.href = Feng.ctxPath + '/inventoryPrice'
            }else {
                Feng.error( data.message)
            }
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/inventoryPrice'
    });

    // 点击门店时
    $('#shopName').click(function () {
        var formName = encodeURIComponent("parent.InventoryPriceInfoDlg.data.shopName");
        var formId = encodeURIComponent("parent.InventoryPriceInfoDlg.data.shopTreeId");
        var treeUrl = encodeURIComponent("/btyShopTree/tree");

        layer.open({
            type: 2,
            title: '门店选择',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#shopTreeId").val(InventoryPriceInfoDlg.data.shopTreeId);
                $("#shopName").val(InventoryPriceInfoDlg.data.shopName);
            }
        });
    });


    /**
     * 初始化品类
     */
    var languageAjax = new $ax(Feng.ctxPath + "/itemType/list?itemTypeCode=101");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        $("#itemTypeCode").append("<option value='" + val.itemTypeCode + "'>" + val.itemTypeName + "</option>");
    });
    $('#itemTypeCode').val(result.data.itemTypeCode);
    form.render('select');

    //初始化品项
    var languageAjax = new $ax(Feng.ctxPath + "/itemInterface/list?itemTypeId=a900c49bbe614c739628fa337efcb556");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        $("#itemCode").append("<option value='" + val.itemCode + "'>" + val.itemName + "</option>");
    });
    $('#itemCode').val(result.data.itemCode);
    form.render('select');

});